package com.mecuros.common.security.component;

import com.mecuros.common.core.util.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

/**
 * @author yujianbo
 * @Classname CarlosTokenStoreAutoCleanSchedule
 * @Description redis token store 自动配置
 * @Date 2022/4/28 9:10 下午
 */
@Slf4j
@EnableScheduling
@ConditionalOnBean(AuthorizationServerConfigurerAdapter.class)
public class CarlosTokenStoreAutoCleanSchedule {

    /**
     * 每小时执行一致，避免 redis zset 容量问题
     */
    @Scheduled(cron = "@hourly")
    public void doMaintenance() {
        CarlosRedisTokenStore tokenStore = SpringContextHolder.getBean(CarlosRedisTokenStore.class);
        long maintenance = tokenStore.doMaintenance();
        log.debug("清理Redis ZADD 过期 token 数量: {}", maintenance);
    }
}
